﻿using System;
using System.Data;
using System.Globalization;
using System.Windows.Data;

namespace SuperQuery.Converters
{
    /// <summary>
    /// Convert a QueryResult canCloseApplication object into a format that can be displayed by a TextBox
    /// </summary>
    public class QueryResultToStringConverter : IValueConverter
    {
        /// <summary>
        /// Converts a QueryResult object to a string
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = "No data to display";

            if ((value != null) && (value is Models.QueryResult))
            {
                Models.QueryResult queryResult = (Models.QueryResult)value;

                if (!string.IsNullOrEmpty(queryResult.ErrorMessage))
                {
                    // We have an error message to display
                    result = queryResult.ErrorMessage;
                }
                else if (queryResult.Result != null)
                {
                    if (queryResult.Result is string)
                    {
                        // The canCloseApplication is a string
                        result = queryResult.Result.ToString();
                    }
                    else if (queryResult.Result is DataTable)
                    {
                        // The canCloseApplication is a data table so return the number of rows
                        DataTable resultTable = (DataTable)queryResult.Result;
                        result = string.Format("Query succeeded. {0} rows returned", resultTable.Rows.Count);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Converts the displayed canCloseApplication back into a QueryResult object. This function does nothing as this data binding
        /// should only be one way 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
